﻿using Microsoft.Office.Interop.PowerPoint;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using VisualPoint.Controls.Actions;
using VisualPoint.Controls.Commands;
using VisualPoint.Controls.Models;
using VisualPoint.Controls.Views;

namespace VisualPoint.Controls.ViewModels
{
    public partial class ActionListViewModel 
    {
        #region Constructor

        public ActionListViewModel()
        {
            _model = new ActionListModel();

            CreateCommands();
            //_model.Actions.Add(new FileOpenActionModel()
            //{
            //    Action = new FileOpenAction()
            //    {
            //        FileName = "FileName"
            //    }
            //});
        }

        #endregion

        #region Properties

        private Slide _currentSlide;

        private ActionListModel _model;
        public ActionListModel Model
        {
            get
            {
                return _model;
            }
        }

        #endregion

        #region Methods

        internal void BeforeSave()
        {
            if (_currentSlide != null)
            {
                SaveData(_currentSlide);
            }
        }

        public void SlideSelectionChange(Microsoft.Office.Interop.PowerPoint.Slide slide)
        {
            if (_currentSlide != null)
            {
                SaveData(_currentSlide);
            }
            if (slide != null)
            {
                RetrieveData(slide);
            }

            _currentSlide = slide;
        }

        private const string tag = "VisualPointActions";

        private void RetrieveData(Slide slide)
        {
            string data = slide.Tags[tag];
            Model.SetSlideActions(data, false);
        }
        private void SaveData(Slide _currentSlide)
        {
            string data = Model.GetSlideActions();
            try
            {
                if (!string.IsNullOrWhiteSpace(_currentSlide.Tags[tag]))
                {
                    _currentSlide.Tags.Delete(tag);
                }
            }
            catch
            { }
            try
            {
                _currentSlide.Tags.Add(tag, data);
            }
            catch
            {}
        }

        public void RunActions(Slide slide, ActionTime actionTime)
        {
            if (slide != null)
            {
                string data = slide.Tags[tag];
                SlideActions slides = SlideActions.DeSerialize(data);
                if (slides != null)
                {
                    foreach (BaseAction action in slides.Actions)
                    {
                        if (action.ActionTime == actionTime)
                        {
                            try
                            {
                                action.Run();
                            }
                            catch (RunException re)
                            {
                                AddMessage(re);
                            }
                        }
                    }
                }
            }
        }

        public void AddMessage(RunException re)
        {
            Model.Message = re.Message + "\n" + Model.Message;
        }


        #endregion

        #region IWindowConfiguration

        partial void DoWindowConfiguration()
        {
            WindowsConfigurationsWindow window = new WindowsConfigurationsWindow();

            window.ShowDialog();
        }

        #endregion

        #region IDeleteAction

        partial void DoDelete()
        {
            if (Model != null
                && Model.SelectedItem != null)
            {
                int i = Model.SelectedItemIndex;

                Model.Actions.Remove(Model.SelectedItem);
                
                if (i < Model.Actions.Count)
                {
                    Model.SelectedItem = Model.Actions[i];
                }
                else
                {
                    Model.SelectedItem = null;
                }
            }

        }

        #endregion

        #region IEditAction

        partial void DoEdit()
        {
            if (Model != null
                && Model.SelectedItem != null)
            {
                Model.SelectedItem.Edit();
            }
        }


        #endregion

        #region IRun

        partial void DoRun()
        {
            if (Model != null)
            {
                if (Model.SelectedItem == null)
                {
                    if (Model.Actions.Count == 0)
                    {
                        return;
                    }
                    Model.SelectedItem = Model.Actions[0];
                }
                try
                {
                    Model.SelectedItem.GetAction().Run();
                }
                catch (RunException re)
                {
                    AddMessage(re);
                }
                int i = Model.SelectedItemIndex;
                if ((i+1) < Model.Actions.Count)
                {
                    Model.SelectedItem = Model.Actions[i + 1];
                }
                else
                {
                    Model.SelectedItem = null;
                }
            }
        }

        #endregion

        #region ICopyAction

        partial void DoCopy()
        {
            string data = Model.GetSlideActions();

            Clipboard.SetText(data);
        }

        #endregion

        #region IPasteAction

        partial void DoPaste()
        {
            if (Clipboard.ContainsText())
            {
                string data = Clipboard.GetText();
                Model.SetSlideActions(data, true);
            }
        }

        #endregion

        #region IMoveItemUp

        partial void DoMoveItemUp()
        {
            Model.MoveItemUp();
        }
        
        #endregion

        #region IMoveItemDown

        partial void DoMoveItemDown()
        {
            Model.MoveItemDown();
        }

        #endregion


        #region IOpenFileAction

        partial void DoFileOpenAction()
        {
            Model.AddAction(new FileOpenActionModel());
        }

        #endregion

        #region IFileCloseAction

        partial void DoFileCloseAction()
        {
            Model.AddAction(new FileCloseActionModel());
        }

        #endregion

        #region ITextSelectAction

        partial void DoTextSelectAction()
        {
            Model.AddAction(new TextSelectActionModel());
        }

        #endregion

        #region IWindowSelectAction

        partial void DoWindowSelectAction()
        {
            Model.AddAction(new WindowSelectActionModel());
        }

        #endregion

        #region ITextWriteAction

        partial void DoTextWriteAction()
        {
            Model.AddAction(new TextWriteActionModel());
        }


        #endregion

        #region ITextWriteAction

        partial void DoResizeAction()
        {
            Model.AddAction(new ResizeActionModel());
        }

        #endregion
    }
}
